<%--
  Created by IntelliJ IDEA.
  User: sunhao
  Date: 2015/11/2 0002
  Time: 17:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE HTML>

<html>
<head>
    <title>收入订单管理</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%--<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">--%>
    <link href="/static/bui/css/bs3/dpl.css" rel="stylesheet" type="text/css"/>
    <link href="/static/bui/css/bs3/bui.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="doc-content">
    <div class="panel">
        <div class="panel-header">
            <h3>收据信息</h3>
        </div>
        <div class="panel-body">
            <form id="searchForm" class="form-horizontal well" action="/income/find" method="post">
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label">学生姓名：</label>

                        <div class="controls">
                            <select name="student" class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="">请选择</option>
                                <c:forEach items="${students}" var="p">
                                    <option value="${p._id}">${p.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label">任课教师：</label>

                        <div class="controls">
                            <select name="teacher" class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="">请选择</option>
                                <c:forEach items="${teachers}" var="p">
                                    <option value="${p._id}">${p.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label">商品类型：</label>

                        <div class="controls">
                            <select name="goodsClass" class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="">请选择</option>
                                <c:forEach items="${goodsClass}" var="p">
                                    <option value="${p._id}">${p.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="row">

                    <div class="control-group span8">
                        <label class="control-label">开始时间：</label>

                        <div class="controls">
                            <input type="text" class="calendar" name="startDate" style="width: auto"/>
                        </div>
                    </div>

                    <div class="control-group span8">
                        <label class="control-label">结束时间：</label>

                        <div class="controls">
                            <input type="text" class="calendar" name="endDate" style="width: auto"/>
                        </div>
                    </div>

                    <div class="control-group span8">
                        <label class="control-label">状态：</label>

                        <div class="controls">
                            <select name="status" class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="">请选择</option>
                                <option value="1">已付款</option>
                                <option value="0">未付款</option>
                                <option value="-1">作废</option>
                            </select>
                        </div>
                    </div>

                    <div class="span3 offset2">
                        <input value="搜索" type="submit" id="btnSearch" class="button button-primary"/>
                    </div>
                </div>
            </form>

            <form id="J_Form" class="form-horizontal well" method="post" style="height: 320px;display: none">
                <div class="row" style="display:none">
                    <div>
                        <label class="control-label"><s>*</s>id：</label>

                        <div class="controls">
                            <input name="_id" type="text" id="_id" required="required">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>日期</label>

                        <div class="controls">
                            <input type="text" class="calendar" name="date" id="date" style="width: auto"
                                   required="required">
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>收据号：</label>

                        <div class="controls">
                            <input type="text" name="num" id="num" required="required">
                        </div>
                    </div>
                    <div class="control-group spam8">
                        <label class="control-label"><s>*</s>学生：</label>

                        <div class="controls">
                            <select id="student" name="student"
                                    class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false" required="required">
                                <option value="">请选择</option>
                                <c:forEach items="${students}" var="p">
                                    <option value="${p._id}">${p.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label" style="margin-left: 30px"><s>*</s>收入项目：</label>

                        <div style="border: double;padding: 30px;width: 800px;margin-left: 150px;margin-bottom: 10px">
                            <div id="action_select" class="controls">
                                <c:forEach items="${actions}" var="p">
                                    <label style="margin-left: 50px">
                                        <input id="${p.index}" name="action" type="radio" value="${p._id}"
                                               required="required"/>${p.name}
                                        <label style="display: none;">${p.item}</label>
                                    </label>
                                </c:forEach>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row" id="item_kefei" style="display: none">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>课程：</label>

                        <div class="controls">
                            <select onchange="changeLesson()" id="lesson" name="lesson"
                                    class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="">请选择</option>
                                <c:forEach items="${lessons}" var="p">
                                    <option value="${p._id}" name="${p.price}">${p.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>教师：</label>

                        <div class="controls">
                            <select id="teacher" name="teacher"
                                    class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                                <option value="">请选择</option>
                                <c:forEach items="${teachers}" var="p">
                                    <option value="${p._id}">${p.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row" id="item_goods" style="display: none">
                    <div class="control-group span8">
                        <label class="control-label" style="padding-left: 30px"><s>*</s>商品种类：</label>

                        <div class="controls">
                            <select id="goodsClass" name="goodsClass" onchange="changGoodsClass()"
                                    class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                            </select>
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>具体商品：</label>

                        <div class="controls">
                            <select id="goods" name="goods" onchange="changeGoods()"
                                    class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false">
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>价格：</label>

                        <div class="controls">
                            <input type="text" name="price" id="price" value=0 required="required">
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>数量：</label>

                        <div class="controls">
                            <input type="text" name="n" id="n" value=0 required="required">
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>合计：</label>

                        <div class="controls">
                            <input type="text" name="total" id="total" readonly>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>收据状态：</label>

                        <div class="controls">
                            <select id="status" name="status" class="input-normal bui-form-field-select bui-form-field"
                                    aria-disabled="false" aria-pressed="false" required="required" >
                                <option value="1">已付</option>
                                <option value="0">未付</option>
                            </select>
                        </div>
                    </div>
                    <div class="control-group span8">
                        <label class="control-label"><s>*</s>管理人员：</label>

                        <select id="manager" name="manager" class="input-normal bui-form-field-select bui-form-field"
                                aria-disabled="false" aria-pressed="false" required="required" >
                            <option value="">请选择</option>
                            <c:forEach items="${managers}" var="p">
                                <option value="${p._id}">${p.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div id="add_button" class="span3 offset2" style="padding-left: 600px;display: none">
                    <input value="确认添加收入" type="button" class="button button-primary"/>
                </div>
                <div id="cancel_button" class="span3 offset2" style="float: left;margin-left: 20px;display: none">
                    <input value="取消添加收入" type="button" class="button button-primary"/>
                </div>
                <div id="edit_button" class="span3 offset2" style="padding-left: 600px;display: none">
                    <input value="确认修改收入" type="button" class="button button-primary"/>
                </div>
            </form>

            <div class="row">
                <div class="control-group span8" style="padding-left: 120px;float: right">
                    <label class="control-label">总计：<span>${incomeCount}</span>元</label>
                </div>
            </div>

            <div class="row">
                <div class="span24 offset3 control-row-auto">
                    <div id="grid"></div>
                    <input type="hidden" name="lesson">
                </div>
            </div>

            <script type="text/javascript" src="/static/bui/js/jquery-1.8.1.min.js"></script>
            <script type="text/javascript" src="/static/bui/js/bui.js"></script>
            <script type="text/javascript" src="/static/bui/js/config.js"></script>
            <script type="text/javascript">

                function changeLesson() {
                    $('#price').val($('#lesson').find("option:selected").attr("name"))
                }

                function changeGoods() {
                    $('#price').val($('#goods').find('option:selected').attr('name'))
                }

                function changGoodsClass() {
                    var class_name = $('#goodsClass option:selected').text()
                    $.each(${goods}, function () {
                        if (this.name == class_name) {
                            $('#goods option:last-child').after('<option value="' + this._id + '" name="' + this.price + '">' + this.num + '</option>')
                        }
                    })
                }

                $(document).ready(function () {
                    $('#add_button_bui').click(function () {
                        init()
                        $('#J_Form,#add_button,#cancel_button').show()
                    })

                    function init() {
                        $('#J_Form,#add_button,#edit_button,#item_goods,#item_kefei').hide()
                    }

                    function init_item() {
                        $('#item_goods,#item_kefei').hide()
                        $('#goodsClass option,#goods option').remove()
                        $('#goodsClass ,#goods').html('<option value="">请选择</option>')
                    }

                    $(":radio").each(function () {
                        $(this).click(function () {
                            init_item()
                            if ($(this).next().text() == '教学收入' ||
                                    $(this).val() == 'de7b2543-60cf-493e-93f2-7ceef785cd3d') {
                                $('#item_kefei').show()
                            }
                            if ($(this).next().text() == '商品收入') {
                                $('#item_goods').show()
                                if ($(this).val() == '659c0223-5bba-463b-ae8d-aef635e24aa8') {
                                    $.each(${goodsClass}, function () {
                                        if (this.group == '教材' && this.count > 0) {
                                            $('#goodsClass option:last-child').after('<option value="' + this._id + ' ">' + this.name + '</option>')
                                        }
                                    })
                                }
                                if ($(this).val() == '41f4b899-0755-400c-8c60-ac934e056731') {
                                    $.each(${goodsClass}, function () {
                                        if (this.group != '教材' && this.group != '耗材' && this.count > 0) {
                                            $('#goodsClass option:last-child').after('<option value="' + this._id + ' ">' + this.name + '</option>')
                                        }
                                    })
                                }
                                if ($(this).val() == 'bf11794a-8444-4fe9-b3bd-511bd8180546') {
                                    $.each(${goodsClass}, function () {
                                        if (this.group == '耗材') {
                                            $('#goodsClass option:last-child').after('<option value="' + this._id + ' ">' + this.name + '</option>')
                                        }
                                    })
                                }
                            }
                        })
                    })


                    $('#total').click(function () {
                        var n = $('#n').val(), price = $('#price').val()
                        if (n > 0 && price > 0) {
                            $('#total').val(n * price)
                        } else {
                            alert("请正确输入价格和数量")
                        }
                    })

                    $('#add_button').click(function () {
                        var date = $('#date').val(), num = $('#num').val(), student = $('#student').val(), status = $('#status').val(),
                                action = $('input:radio:checked').val(), price = $('#price').val(), n = $('#n').val(),
                                total = $('#total').val(), lesson = $('#lesson').val(), teacher = $('#teacher').val(),
                                goodsClass = $('#goodsClass').val().trim(), goods = $('#goods').val(), manager = $('#manager').val()
                        $.ajax({
                            type: 'post',
                            url: '/income/addIncome',
                            data: {
                                date: date,
                                num: num,
                                student: student,
                                action: action,
                                price: price,
                                n: n,
                                total: total,
                                lesson: lesson,
                                teacher: teacher,
                                goodsClass: goodsClass,
                                goods: goods,
                                manager: manager,
                                status: status
                            },
                            success: function (msg) {
                                if (msg == 'success') {
                                    msg = "添加成功！";
                                    BUI.Message.Alert(msg, function () {
                                        window.location.href = '/income/list.do';
                                    }, 'success')
                                } else {
                                    msg = "添加失败";
                                    BUI.Message.Alert(msg, function () {
                                        window.location.href = '/jsp/error/noAuthority.jsp';
                                    }, 'error')
                                }
                            }
                        })
                    })

                    $('#cancel_button').click(function () {
                        $('#J_Form,#add_button,#cancel_button').hide()
                    })

                })

                BUI.use('bui/calendar', function (Calendar) {
                    var datepicker = new Calendar.DatePicker({
                        trigger: '.calendar',
                        autoRender: true
                    });
                });

                BUI.use(['bui/grid', 'bui/data', 'bui/form'], function (Grid, Data, Form) {
                    var columns = [
                                {title: '日期', dataIndex: 'date', width: '15%'},
                                {title: '收据号', dataIndex: 'num', width: '15%'},
                                {title: '学生姓名', dataIndex: 'student', width: '10%'},
                                {title: '交易', dataIndex: 'action', width: '25%'},
                                {title: '状态', dataIndex: 'status', width: '10%'},
                                {title: '金额（单位元）', dataIndex: 'pay', width: '15%'}

                            ],
                    //默认的数据
                            data = [
                                <c:forEach items="${incomes}" var="r">
                                {
                                    _id: '${r._id}',
                                    num: '${r.num}',
                                    student: '${r.student}',
                                    action: '${r.action}',
                                    date: '${r.date}',
                                    pay: '${r.pay}',
                                    status: '<c:if test="${r.status==1}">已付款</c:if><c:if test="${r.status==0}">未付款</c:if><c:if test="${r.status==-1}">作废</c:if>'
                                },
                                </c:forEach>
                            ],
                            store = new Data.Store({
                                data: data,
                                pageSize: 10
                            }),
                            editing = new Grid.Plugins.DialogEditing({
                                contentId: 'content',
                                triggerCls: 'btn-edit',
                                editor: {
                                    focusable: false,
                                    success: function () {
                                        var editType = editing.get('editType'),
                                                edtor = this,
                                                form = edtor.get("form");
                                        form.valid();
                                        if (form.isValid()) {
                                            this.close();
                                        }
                                    }
                                }
                            }),
                            grid = new Grid.Grid({
                                render: '#grid',
                                columns: columns,
                                width: '100%',
//                    forceFit: true,
                                plugins: [Grid.Plugins.AutoFit],
                                store: store,
                                bbar: {
                                    pagingBar: true,
                                    elCls: 'image-pbar'
                                },
                                plugins: [Grid.Plugins.CheckSelection, editing],
                                tbar: {
                                    items: [{
                                        id: 'add_button_bui',
                                        btnCls: 'button button-small',
                                        text: '<i class="icon-plus"></i>添加收入信息'
                                    },
                                        {
                                            btnCls: 'button button-small',
                                            text: '<i class="icon-remove"></i>作废收入信息',
                                            listeners: {
                                                'click': delFunction
                                            }
                                        }
//                                        ,
//                                        {
//                                            id: 'edit_button_bui',
//                                            btnCls: 'button button-small',
//                                            text: '<i class="icon-circle-arrow-left"></i>编辑收入信息',
//                                            listeners: {
//                                                'click': editFunction
//                                            }
//                                        }

                                    ]
                                }
                            });
                    grid.render();


                    function editFunction() {
                        var selections = grid.getSelection()
                        var _id
                        if (selections.length != 1) {
                            BUI.Message.Confirm('需要选择一条信息后才能修改，请勿多选或漏选', function () {
                                init()
                            })
                        }
                        init()
                        $('#J_Form,#edit_button').show()
                        $('#edit_button').click(function () {
                            BUI.each(selections, function (item) {
                                _id = item._id
                            })
                            var date = $('#date').val(), num = $('#num').val(), student = $('#student').val(), status = $('#status').val(),
                                    action = $('input:radio:checked').val(), price = $('#price').val(), n = $('#n').val(),
                                    total = $('#total').val(), lesson = $('#lesson').val(), teacher = $('#teacher').val(),
                                    goodsClass = $('#goodsClass').val(), goods = $('#goods').val(), manager = $('#manager').val()
                            $.ajax({
                                type: 'post',
                                url: '/income/updateIncome',
                                data: {
                                    _id: _id,
                                    date: date,
                                    num: num,
                                    student: student,
                                    action: action,
                                    price: price,
                                    n: n,
                                    total: total,
                                    lesson: lesson,
                                    teacher: teacher,
                                    goodsClass: goodsClass,
                                    goods: goods,
                                    manager: manager,
                                    status: status
                                },
                                success: function (msg) {
                                    if (msg == 'success') {
                                        msg = "修改成功！";
                                        BUI.Message.Alert(msg, function () {
                                            window.location.href = '/income/list.do';
                                        }, 'success')
                                    } else {
                                        msg = "修改失败";
                                        BUI.Message.Alert(msg, function () {
                                            window.location.href = '/jsp/error/noAuthority.jsp';
                                        }, 'error')
                                    }
                                }
                            })
                        })
                    }


                    function init() {
                        $('#J_Form,#add_button,#edit_button,#item_goods,#item_kefei').hide()
                    }


                    function init_item() {
                        $('#item_goods,#item_kefei').hide()
                    }

                    function delFunction() {
                        var selections = grid.getSelection()
                        delItems(selections)
                    }

                    function delItems(items) {
                        var _ids = [];
                        BUI.each(items, function (item) {
                            _ids.push(item._id)
                        });
                        if (_ids.length) {
                            BUI.Message.Confirm('确认要作废收入信息吗？', function () {
                                $.ajax({
                                    type: 'POST',
                                    url: '/income/cancelIncome?_ids=' + _ids,
                                    success: function (msg) {
                                        if (msg == 'success') {
                                            msg = "操作成功！";
                                            BUI.Message.Alert(msg, function () {
                                                window.location.href = '/income/list.do';
                                            }, 'success')
                                        } else {
                                            msg = '操作失败'
                                            BUI.Message.Alert(msg, function () {
                                                window.location.href = '/jsp/error/noAuthority.jsp';
                                            }, 'error')
                                        }
                                    }
                                })
                            })
                        }
                    };
                    var form = new Form.HForm({
                        srcNode: '#J_Form'
                    })
                })
            </script>
        </div>
    </div>
</div>
</body>
</html>

